﻿using MeetingRoomBooking.Application.Common.Interfaces;

namespace MeetingRoomBooking.Application.Rooms.Commands;
public class UpdateRoomCommand : IRequest
{
    public int Id { get; set; }
    public required string Name { get; set; }
    public string? Description { get; set; }
    public int Capacity { get; set; }
}

public class UpdateRoomCommandHandler : IRequestHandler<UpdateRoomCommand>
{
    private readonly IApplicationDbContext _context;

    public UpdateRoomCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }
    public async Task Handle(UpdateRoomCommand request, CancellationToken cancellationToken)
    {
        var room = await _context.Rooms.FindAsync(request.Id);

        if (room == null) throw new Exception($"Room Id:{request.Id} not found.");

        room.Name = request.Name;
        room.Description = request.Description;
        room.Capacity = request.Capacity;

        await _context.SaveChangesAsync(cancellationToken);
    }
}
